local Msg = require("core.Msg")
local Util = require("common.Util")
local Lang = require("common.Lang")
local HtmlUtil  = require("common.HtmlUtil")
local FirstPayConfig = require("excel.Fuli").firstPay
local ItemLogic = require("bag.ItemLogic")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local RoleUIControl = require("role.RoleUIControl")
local Broadcast = require("broadcast.Broadcast")
local PanelDefine = require("broadcast.PanelDefine")
require("admin.PayLogicL")
local Banshu = require("common.Banshu")

function query(human, type)
    if type < 1 or type > 2 then
        return
    end
    if human.db.rmb == 0 and type == 2 then
        return
    end
    local msg = Msg.gc.GC_FIRST_PAY_QUERY
    msg.type = type
    local id
    local desc
    if type == 1 then
        id = FirstPayConfig[0].first_reward
        desc = FirstPayConfig[0].first_reward_desc
    else
        id = FirstPayConfig[0].first_day
        desc = FirstPayConfig[0].first_day_desc
    end
    msg.reward[0] = 0
    msg.desc[0] = 0
    local grids = ItemLogic.getLiabaoShowGrid(id)
    if grids then
        local len = 0
        for i = 1, #grids do
            if (grids[i].sbid == nil or grids[i].sbid == 0 or grids[i].sbid == human.db.shenbing[0].id) and (grids[i].sex == nil or grids[i].sex == 0 or grids[i].sex == human.db.sex) then
              len = len + 1
              Grid.makeItem(grids[i], msg.reward[len], 0, human)
              msg.desc[len] = desc[len] or 0
              if len == 10 then
                break
              end
            end
        end
        msg.reward[0] = len
        msg.desc[0] = len
    end
    
    msg.sbId = human.db.shenbing[0].id
    
    --状态
    if human.db.rmb == 0 then
        msg.status = 0
    else
        if type == 1 then
            if Util.getBit(human.db.firstPay, 0) == 1 then
                  msg.status = 1
            else
                  msg.status = 2
            end
        else
            if Util.getBit(human.db.firstPay, 1) == 1 then
                  msg.status = 1                  
            elseif human.db.everyDayPay >= 100 then
                  msg.status = 2
            else
                  msg.status = 0
            end
        end
    end
    Msg.send(msg, human.fd)    
end

-- 领取充值礼包
function get(human, type)
    if type < 1 or type > 2 then
        return
    end
    if human.db.rmb == 0 then
        return
    end
    if BagLogic.getLeftGrids(human) < 1 then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.SHOP_ERR_BAG_FULL)
        return
    end
    if type == 1 then
        if Util.getBit(human.db.firstPay, 0) == 0 then
            return -- 已领取
        end
        human.db.firstPay = human.db.firstPay - 1
        BagLogic.addItem(human, FirstPayConfig[0].first_reward, true, 1, "pay")
        Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, HtmlUtil.fontName(human.name, human._id)..Lang.FIRST_PAY_REWARD_GET1)
    else
        if human.db.everyDayPay < 100 then 
            return -- 未达到
        end
        if Util.getBit(human.db.firstPay, 1) == 0 then
            return -- 已领取
        end
        human.db.firstPay = human.db.firstPay - 2
        BagLogic.addItem(human, FirstPayConfig[0].first_day, true, 1, "pay")        
        Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, HtmlUtil.fontName(human.name, human._id)..Lang.FIRST_PAY_REWARD_GET2)        
    end
    local msg = Msg.gc.GC_FIRST_PAY_GET
    msg.result = 0
    msg.type = type
    Msg.send(msg, human.fd)
    Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.FULI_PAY_GET_OK)    
    
    RoleUIControl.sendLine1FnIcon(human)
end

function canShowFirstReward(human)
    if Banshu.IS_BANSHU then
      return false
    end
    if human.db.rmb == 0 then
        return true
    end
    if Util.getBit(human.db.firstPay, 0) == 1 then
        return true
    end
end

function canShowFirstDayReward(human)
    if true or human.db.rmb == 0 then
        return 
    end
    
    if Util.getBit(human.db.firstPay, 1) == 1 then
        return true
    end
    if  human.db.everyDayPay < 100 then 
        return true
    end
end

function updateDaily(human)
    human.db.everyDayPay = 0
    if Util.getBit(human.db.firstPay, 1) == 0 then
        return
    end
    
--    if not libao then
--        libao = {}
--        libao[1] = {}
--        Grid.create(libao[1], FirstPayConfig[0].first_day, 1)
--    end
--    human.db.firstPay = human.db.firstPay - 2
--    MailLogic.CreateNewMailForUser(human._id, Lang.FIRST_PAY_MAIL_TITLE, Lang.FIRST_PAY_MAIL_CONTENT, libao)
end

function openFirstPayPanel(human)
	local msgRet = Msg.gc.GC_OPEN_PANEL
	msgRet.id = PanelDefine.PANEL_FIRST_DELTA
	msgRet.values[0] = 0
	Msg.send(msgRet, human.fd)
end